package com.ktwlsoft.archivemanage.domain.business.businessService.businessInspect;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.businessInspect.BusinessInspectRecordRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.businessInspect.BusinessInspectRecord;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import com.ktwlsoft.archivemanage.service.utils.SnowFlake;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 业务检查记录控制层
 *
 * @author Name on 2023/6/26 15:33
 * @version 1.0
 */
@Component
@RequiredArgsConstructor
public class BusinessInspectRecordService {

    private final BusinessInspectRecordRepositoryService service;

    /**
      * 获取业务检查记录 on 2023/6/26 16:35
      *
      * @param userId userId
      * @param businessType businessType
      * @return Result<?>
      */
    public Result<?> getBusinessInspectRecord(Long userId,String businessType){
        List<BusinessInspectRecord> records = service.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(userId),BusinessInspectRecord::getBusinessId,userId)
                .eq(StrUtil.isNotBlank(businessType),BusinessInspectRecord::getBusinessType,businessType)
                .list();
        return Result.ok(records);
    }

    /**
      * 新增业务检查记录 on 2023/6/26 16:35
      *
      * @return Result<?>
      */
    public Result<?> save(BusinessInspectRecord record){
        Account account = UserPrincipal.getCurrentUser();
        if(ObjectUtil.isEmpty(account)){
            return Result.body(record,ResultType.CHECK_ERROR).withMessage("获取不到当前用户");
        }
        record.setId(SnowFlake.getNextId())
                .setCreator(account.getRealName())
                .setCreatorId(account.getId())
                .setCreatedAt(new Date())
                .setUpdateKey(account.getRealName())
                .setUpdateId(account.getId())
                .setUpdateAt(new Date());
        service.save(record);
        return Result.ok(record.getId());
    }

    /**
      * 新增业务检查记录 on 2023/6/26 16:35
      *
      * @return Result<?>
      */
    public Result<?> edit(BusinessInspectRecord record){
        Account account = UserPrincipal.getCurrentUser();
        if(ObjectUtil.isEmpty(account)){
            return Result.body(record,ResultType.CHECK_ERROR).withMessage("获取不到当前用户");
        }
        if(ObjectUtil.isEmpty(record.getId())){
            return Result.body(record,ResultType.CHECK_ERROR).withMessage("id不能为空");
        }
        record.setUpdateKey(account.getRealName())
                .setUpdateId(account.getId())
                .setUpdateAt(new Date());
        service.updateById(record);
        return Result.ok(record.getId());
    }

    /**
     * 删除业务检查记录 on 2023/6/26 16:35
     *
     * @return Result<?>
     */
    public Result<?> delete(String id){
        BusinessInspectRecord record = service.getById(id);
        if(ObjectUtil.isEmpty(record)){
            return Result.body(id,ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        service.removeById(record);
        return Result.ok(record.getId());
    }

    /**
     * 启用禁用 on 2023/6/26 16:35
     *
     * @return Result<?>
     */
    public Result<?> editStatus(String id){
        BusinessInspectRecord record = service.getById(id);
        if(ObjectUtil.isEmpty(record)){
            return Result.body(id,ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        service.lambdaUpdate()
                .eq(BusinessInspectRecord::getId,id)
                .set(BusinessInspectRecord::getStatus,record.getStatus().equals(1)?0:1)
                .update();
        return Result.ok(record.getId());
    }

    /**
     * 分页 on 2023/6/26 16:35
     *
     * @return Result<?>
     */
    public Result<?> findPage(int pn,int ps,Long userId,String businessType){
        Page<BusinessInspectRecord> page = new Page<>(pn,ps);
        page = service.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(userId),BusinessInspectRecord::getBusinessId,userId)
                .eq(StrUtil.isNotBlank(businessType),BusinessInspectRecord::getBusinessType,businessType)
                .page(page);
        return Result.ok(page);
    }
}